package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.entity.vo.SetmealDishVo;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto, HttpSession session){
        setmealDto.setCreateUser((Long) session.getAttribute("employee"));
        setmealDto.setUpdateUser((Long) session.getAttribute("employee"));

        setmealService.add(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer pageSize,
                  String name){
        Page<SetmealDto> setmealDtoPage = setmealService.page(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    public R del(@RequestParam List<Long> ids){
        setmealService.delByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R lst(Long categoryId, Integer status){
         List<Setmeal> setmealList = setmealService.lst(categoryId, status);
         return R.success(setmealList);
    }
    @GetMapping("/dish/{setmealId}")
    public R setmealDishLst(@PathVariable Long setmealId){
        List<SetmealDishVo> setmealDishList = setmealService.getSetmealDish(setmealId);
        return R.success(setmealDishList);
    }
}
